//
//  GRSellAfaterViewController.m
//  YC_GRice
//
//  Created by luojinrong11 on 2018/7/25.
//  Copyright © 2018年 YeCai. All rights reserved.
//

#import "GRSellAfaterViewController.h"
#import "GRSellAfterCell.h"
#import "RequestManager+GROrderManager.h"
#import "GROrderReturnModel.h"
#import "GRReturnViewController.h"
#import "GRBlankSpace.h"
@interface GRSellAfaterViewController ()<UIScrollViewDelegate,UITableViewDelegate,UITableViewDataSource>
{
    NSInteger applyCurrentIndex;
    NSInteger recordCurrentIndex;
}
@property (weak, nonatomic) IBOutlet UITableView *applyTableView;

@property (weak, nonatomic) IBOutlet UITableView *recordTableView;

@property (weak, nonatomic) IBOutlet UIView *lineView;

@property (weak, nonatomic) IBOutlet UIScrollView *mainScroller;
@property (weak, nonatomic) IBOutlet UIView *mainTitleView;
@property (nonatomic,weak)UIButton * selectBt;
@property(nonatomic,strong)NSMutableArray * applyDataArr;
@property(nonatomic,strong)NSMutableArray * recordArr;
@property(nonatomic,strong)GRBlankSpace * applyBlankSpceView;
@property(nonatomic,strong)GRBlankSpace * recordBlankSpceView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topOffset;

@end

@implementation GRSellAfaterViewController
-(GRBlankSpace *)applyBlankSpceView{
    if (!_applyBlankSpceView) {
        _applyBlankSpceView = [GRBlankSpace xx_instantiateFromNib];
        _applyBlankSpceView.contentMode =  GRBlankContentModeEmptyOrders ;
        _applyBlankSpceView.frame = CGRectMake(0, 0, self.applyTableView.bounds.size.width, self.applyBlankSpceView.bounds.size.height );
        [self.applyTableView addSubview:_applyBlankSpceView];
    }
    return _applyBlankSpceView;
}

-(GRBlankSpace *)recordBlankSpceView {
    if (!_recordBlankSpceView) {
        _recordBlankSpceView = [GRBlankSpace xx_instantiateFromNib];
        _recordBlankSpceView.contentMode =  GRBlankContentModeEmptyOrders;
        _recordBlankSpceView.frame = self.recordTableView.bounds ;
        [self.recordTableView addSubview:_recordBlankSpceView];
    }
    return _recordBlankSpceView;
    
}
-(NSMutableArray *)applyDataArr {
    if (!_applyDataArr) {
        _applyDataArr = [NSMutableArray array];
    }
    return _applyDataArr;
}

-(NSMutableArray *)recordArr{
    if (!_recordArr) {
        _recordArr = [NSMutableArray array];
    }
    return _recordArr;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.topOffset.constant = IS_IPHONEX ?88:64;
    _selectBt = (UIButton *)[self.mainTitleView viewWithTag:100];
    [self.applyTableView registerNib:[UINib nibWithNibName:@"GRSellAfterCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"afterCell"];
    [self.recordTableView registerNib:[UINib nibWithNibName:@"GRSellAfterCell" bundle:[NSBundle mainBundle]] forCellReuseIdentifier:@"afterCell"];
    self.applyTableView.tableFooterView = [[UIView alloc]init];
    self.recordTableView.tableFooterView = [[UIView alloc]init];
    [self loadData];
    
}
-(void)requestApply:(GRRefresh)freshType ListType:(NSInteger)listType{
    NSDictionary * dic = @{
                           @"currentPage":@( listType == 1?applyCurrentIndex:recordCurrentIndex),
                           @"pageSize":@(10),
                           @"returnOrder":listType == 1? @"half":@"all"
                           };
    [RequestManager OrderReturnList:dic CompleteHander:^(BOOL success, id responseObject, GRError *error) {
        if (success) {
            if (freshType == GRRefreshForHeader) {
                if(listType == 1){
                    [self.applyDataArr removeAllObjects];
                }else{
                    [self.recordArr removeAllObjects];
                }
            }
            NSArray * list = responseObject[@"dataList"];
            if (Array(list)) {
                for (NSDictionary * dic in list) {
                    GROrderReturnModel  * returnModel = [GROrderReturnModel yy_modelWithDictionary:dic];
                    if (listType == 1) {
                        [self.applyDataArr addObject:returnModel];
                    }else{
                        [self.recordArr addObject:returnModel];
                    }
                }
  
            }
        }
        if (listType == 1) {
            self.applyBlankSpceView.hidden = self.applyDataArr.count >0?YES:NO;
            [self.applyTableView reloadData];
        }else{
            self.recordBlankSpceView.hidden = self.recordArr.count >0 ?YES:NO;
            [self.recordTableView reloadData];
        }
        [self.applyTableView.mj_footer endRefreshing];
        [self.applyTableView.mj_header endRefreshing];
        [self.recordTableView.mj_footer endRefreshing];
        [self.recordTableView.mj_header endRefreshing];
    }];
    
}

-(void)loadData{
    WEAKSELF
    self.applyTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        applyCurrentIndex = 1;
        [weakSelf requestApply:GRRefreshForHeader ListType:1];
    }];
    self.applyTableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        applyCurrentIndex ++;
        [weakSelf requestApply:GRRefreshForFooter ListType:1];
        
    }];
    self.recordTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        recordCurrentIndex = 1;
        [weakSelf requestApply:GRRefreshForHeader ListType:2];
    }];
    self.applyTableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        recordCurrentIndex ++;
        [weakSelf requestApply:GRRefreshForFooter ListType:2];
    }];
    [self.applyTableView.mj_header beginRefreshing];
    [self.recordTableView.mj_header beginRefreshing];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    if ([scrollView isEqual:self.mainScroller]) {
        NSInteger  index = (int)(scrollView.contentOffset.x/KscreenWidth +0.5);
        self.lineView.centerX = KscreenWidth/4 + scrollView.contentOffset.x/2;
        _selectBt.selected= NO;
        UIButton * nowSeletedBt = [self.mainTitleView viewWithTag:100+index];
        nowSeletedBt.selected = YES;
        _selectBt = nowSeletedBt;
        
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    GROrderReturnModel *returnModel;
    if ([tableView isEqual:self.applyTableView]) {
          returnModel = self.applyDataArr[indexPath.row];
    }else{
          returnModel = self.recordArr[indexPath.row];
    }
    GRReturnViewController *returnVC = [[GRReturnViewController alloc]init];
        returnVC.orderId = returnModel.orderInfoId;
        returnVC.isReturnGood = YES;
    WEAKSELF
       returnVC.returnSuccess = ^{
           [weakSelf.applyTableView.mj_header beginRefreshing];
           [weakSelf.recordTableView.mj_header beginRefreshing];
       };
    [self.navigationController pushViewController:returnVC animated:YES];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    GRSellAfterCell * sellAfterCell = [tableView dequeueReusableCellWithIdentifier:@"afterCell" forIndexPath:indexPath];
    if ([tableView isEqual:self.applyTableView]) {
        sellAfterCell.returnModel = self.applyDataArr[indexPath.row];
    }else{
        sellAfterCell.returnModel = self.recordArr[indexPath.row];
    }
    return sellAfterCell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 187;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if ([tableView isEqual:self.applyTableView] ) {
        return self.applyDataArr.count;
    }else{
        return  self.recordArr.count;
    }
}


- (IBAction)seletedAction:(UIButton *)sender {
 
    [self.mainScroller setContentOffset:CGPointMake((sender.tag-100)*KscreenWidth, 0) animated:YES];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
